package com.estudy.config;

import com.estudy.interceptor.JwtInterceptor;
import com.estudy.utils.JwtTokenUtil;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * jwt 后台访问拦截
 * 拦截器配置文件 config
 *
 * @author laisir
 * @date 2024/8/28
 */
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {

    @Bean
    public JwtInterceptor JwtInterceptor() {
        return new JwtInterceptor();
    }

    // 这个方法用来注册拦截器，写好的拦截器需要通过这里添加注册才能生效
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册拦截器
        InterceptorRegistration registration = registry.addInterceptor(JwtInterceptor());
        registration.addPathPatterns("/**");//所有路径都被拦截
        registration.excludePathPatterns(
                "/user/login",                // 登录接口不拦截
                "/user/logout",               // 登出接口不拦截
                "/user/register",             // 注册接口不拦截
                "/assets/**",             // assets文件夹里文件不拦截
                "/**/*.js",              //js静态资源不拦截
                "/**/*.css"             //css静态资源不拦截
        );
    }
}